package com.ruicar.afs.cloud.manage.contractcancel.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.manage.common.entity.ManageCancelRecord;
import com.ruicar.afs.cloud.manage.common.service.ManageCancelRecordService;
import com.ruicar.afs.cloud.manage.contractcancel.condition.ManageAccountRegisterCondition;
import com.ruicar.afs.cloud.manage.contractcancel.vo.ContractCancelManageVO;
import com.ruicar.afs.cloud.manage.finance.bankarrive.entity.ManageAccountRegister;
import com.ruicar.afs.cloud.manage.finance.bankarrive.service.ManageAccountRegisterService;
import com.ruicar.afs.cloud.manage.insurance.entity.ManageInsuranceInfo;
import com.ruicar.afs.cloud.manage.workflow.processor.ApproveTaskProcessor;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: xudongdong
 * @date: 2020/7/3
 * @description: 合同取消信息查询
 */
@Slf4j
@RestController
@RequestMapping("/contractCancel")
@AllArgsConstructor
public class ContractCancelManageController {

    private final ApproveTaskProcessor approveTaskProcessor;
    private final ManageCancelRecordService manageCancelRecordService;
    private final ManageAccountRegisterService manageAccountRegisterService;

    @ApiOperation(value = "查询合同取消列表数据")
    @PostMapping("/findContractCancelList")
    public IResponse findContractCancelList(@RequestBody ContractCancelManageVO vo) {
        vo.setProcessPerson(SecurityUtils.getUser().getUsername());
        Page page = new Page(vo.getPageNumber(), vo.getPageSize());
        IPage list = manageCancelRecordService.getContractCancelList(page, vo);
        list.setRecords(approveTaskProcessor.handle(list.getRecords()));
        return new IResponse().setData(list);
    }

    @ApiOperation(value = "查询合同取消面详细数据")
    @PostMapping("/findByIdDetails")
    public IResponse findByIdDetails(@RequestParam("caseNo") String caseNo) {
        ManageCancelRecord manageCancelRecord = manageCancelRecordService.getOne(Wrappers.<ManageCancelRecord>lambdaQuery()
                .eq(ManageCancelRecord::getCaseNo, caseNo));
        return new IResponse().setData(manageCancelRecord);
    }

    @PostMapping("/submit")
    @ApiOperation(value = "保存合同取消信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<ManageCancelRecord> submit(@RequestBody ContractCancelManageVO manageCancelRecord) {
        manageCancelRecordService.saveCancelRecord(manageCancelRecord);
        return new IResponse<ManageCancelRecord>().setData(manageCancelRecord);
    }

    @PostMapping("/submitCancelRecord")
    @ApiOperation(value = "提交合同取消业务信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse submitCancelRecord(@RequestBody ContractCancelManageVO manageCancelRecord) {
        manageCancelRecordService.submitCancelRecord(manageCancelRecord);
        return new IResponse<Boolean>().setData(true).setMsg("流程提交成功！");
    }

    @PostMapping("/saveProposalApplicationInfo")
    @ApiOperation(value = "流程审批操作")
    @Transactional(rollbackFor = Exception.class)
    public IResponse saveProposalApplicationInfo(@RequestBody ContractCancelManageVO manageCancelRecord) {
        Boolean flag = manageCancelRecordService.saveProposalApplicationInfo(manageCancelRecord);
        return new IResponse<Boolean>().setData(flag).setMsg("流程提交成功！");
    }

    @SysLog("删除合同取消业务信息")
    @ApiOperation(value = "通过id删除")
    @RequestMapping(value = "/delById/{id}", method = RequestMethod.DELETE)
    @Transactional(rollbackFor = Exception.class)
    public IResponse<Boolean> delById(@PathVariable String id) {
        manageCancelRecordService.removeById(id);
        return new IResponse<Boolean>().setMsg("删除成功");
    }

    @ApiOperation(value = "查询银行到账记录表数据")
    @PostMapping("/getArrivalList")
    public IResponse getArrivalList(@RequestBody ManageAccountRegisterCondition account) {
        IPage<ManageAccountRegister> list = manageAccountRegisterService.page(new Page(account.getPageNumber(), account.getPageSize()), Wrappers.<ManageAccountRegister>query().lambda()
                .eq(EmptyUtils.isNotEmpty(account.getReceivingBank()), ManageAccountRegister::getReceivingBank, account.getReceivingBank())
                .like(EmptyUtils.isNotEmpty(account.getReceivedAmt()), ManageAccountRegister::getReceivedAmt, account.getReceivedAmt())
                .like(EmptyUtils.isNotEmpty(account.getPaymentCust()), ManageAccountRegister::getPaymentCust, account.getPaymentCust())
                .eq(EmptyUtils.isNotEmpty(account.getArrivalTime()), ManageAccountRegister::getArrivalTime, account.getArrivalTime())
                .eq(EmptyUtils.isNotEmpty(account.getStatus()), ManageAccountRegister::getStatus, account.getStatus()));
        return new IResponse().setData(list);
    }

    @ApiOperation(value = "查询已勾稽到账记录列表数据")
    @PostMapping("/getArrival")
    public IResponse getArrival(@RequestParam("contractNo") String contractNo) {
        List<ManageAccountRegister> manageAccountRegister = manageAccountRegisterService.list(Wrappers.<ManageAccountRegister>lambdaQuery()
                .eq(ManageAccountRegister::getContractNo, contractNo));
        return new IResponse().setData(manageAccountRegister);
    }

}
